package com.qian.ddj.bill.alipay;

import com.qian.api.ddj.model.Order;
import com.qian.api.ddj.model.Spider;
import com.qian.api.ddj.service.IOrderService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

@Data
@Slf4j
public class Notify implements Runnable {

    private List<Spider> spiders;
    private IOrderService orderService;

    @Override
    public void run() {

        for (Spider spider : spiders) {
            log.info(" *********************** notify " + spider.getOrderInfo());
            if ("交易成功".equals(spider.getStatus()) && spider.getAmount().indexOf("+") != -1) {
                Order order = new Order();
                order.setRemark(spider.getRemark());
                order.setBusinessId(spider.getBusinessId());
                order = orderService.findOne(order);
                if (!ObjectUtils.isEmpty(order)) {
                    try {
                        BigDecimal amount = new BigDecimal(spider.getAmount().split(" ")[1]);
                        if (order.getAmount().compareTo(amount) == 0) {
                            String orderInfo = spider.getOrderInfo();
                            orderInfo = orderInfo.substring(orderInfo.length() - 28, orderInfo.length());
                            order.setUpOrderNo(orderInfo);
                            orderService.orderFinish(order);
                            orderService.notifyToUser(order);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
